package com.telenav.snowsweeper.controller;

import com.telenav.snowsweeper.renderer.MapEdgeRenderer;
import com.telenav.snowsweeper.renderer.Util;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MapEdgeTouchEventHandler implements OnTouchListener {
	
	private View target;
	
	boolean isDragMode = false;
	private float lastDragX = -1;
	private float lastDragY = -1;
	
	private MapEdgeRenderer.RendererParams params;
	
	public MapEdgeTouchEventHandler(Context context, View target, MapEdgeRenderer.RendererParams params) {
		this.target = target;
		this.params = params;
		target.setOnTouchListener(this);
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		Log.d("tn-debug", event.getAction() + ", " + event.getX() + ", " + event.getY());
		if (v == target) {
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					lastDragX = event.getX();
					lastDragY = event.getY();
					isDragMode = true;
					break;
				case MotionEvent.ACTION_MOVE:
					float dx = (event.getX() - lastDragX) * Util.getPanSensitive();
					float dy = (event.getY() - lastDragY) * Util.getPanSensitive();
					params.move((int)-dx, (int)-dy);
					lastDragX = event.getX();
					lastDragY = event.getY();
					break;
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_CANCEL:
				case MotionEvent.ACTION_OUTSIDE:
					isDragMode = false;
					break;
			}
			return true;
		}
		return false;
	}
}
